import Opinion from "../../model/opinion";
import Service from "../../model/service";
import { debounce } from "../../utils/util";

Page({
  data: {
    tabs: [],
    tabIndex: 0, // 当前tab下标
    projectId: null, // 项目id
    managerId: null, // 项目经理id
    deptType: '0', // 角色类型
    tempParentId: null, //
    firstList: [   // 基本信息
      {name: '项目名称', key: 'projectName', value: ''},
      {name: '立项编号', key: 'approvalNumber', value: ''},
      {name: '所属部门', key: 'deptName', value: ''},
      {name: '委托函函号', key: 'letterNumber', value: ''},
      {name: '评审发文文号', key: 'documentNumber', value: ''},
      {name: '项目分类', key: 'projectClassification', value: ''},
      {name: '委托方式', key: 'entrustmentMethod', value: ''},
      {name: '收文人', key: 'receivingLiterati', value: ''},
      {name: '投资额', key: 'investmentAmount', value: ''},
      {name: '项目编码', key: 'projectCode', value: ''},
      {name: '项目经理', key: 'managerName', value: ''},
      {name: '登记日期', key: 'registDate', value: ''},
      {name: '委托日期', key: 'entrustingDate', value: ''},
      {name: '发文日期', key: 'documentDate', value: ''},
      {name: '项目主管单位', key: 'sponsor', value: ''},
      {name: '委托单位', key: 'client', value: ''},
      {name: '收文日期', key: 'receivingDate', value: ''},
      {name: '项目分级', key: 'projectGrading', value: ''}
        ],
    secondList: {}, // 进度信息
    thirdList: [], // 文件清单
    fourthList: [], // 专家意见
    forumList: [], // 讨论区
    formData: {}, // 专家 专家意见
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      projectId: options.project_id,
      managerId: options.manager_id,
      deptType: options.dept_type, 
      tempParentId: options.tempParent_id
    })
    this.init(options.dept_type)
  },
  // 初始化
  init(type) {
    if(type === '1') {
      // 专家
      this.setData({
        tabs: [{name:'基本信息', key: 0}, {name:'文件清单', key:2}, {name:'专家意见',key: 3}, {name:'讨论区',key: 4}]
      })
    } else if( type === '2' ||type === '3') {
      // 项目单位 委托单位
      this.setData({
        tabs: [{name:'基本信息', key: 0}, {name:'进度信息', key: 1} , {name:'文件清单', key:2}, {name:'专家意见',key: 3}]
      })
    }
    this._getDetail()
    this._getProgress()
    this._getSpecialist()
  },
  // 获取基本信息
  async _getDetail() {
    const res = await Service.getProjectById({projectId: this.data.projectId})
    this.data.firstList.forEach(item =>{
      item.value = res[item.key]
    })
    this.setData({
      firstList: this.data.firstList
    })
  },
  // 获取进度信息
  async _getProgress() {
    let query = {
      projectId: this.data.projectId,
      tempParentId: this.data.tempParentId
    }
    const {data} = await Service.getProgressById(query)
    this.setData({
      secondList: data
    })
  },
  
  // 获取专家意见
  async _getSpecialist() {
    let query = {
      projectId: this.data.projectId, // kns0L5WNYZ90NPxbXegMpZqnk5Uh4MFL
    }
    const {data} = await Service.getSpecialist(query)
    this.setData({
      fourthList: data
    })
  },
  // 专家---获取意见
  async _getMessageAPI() {
    let {data} = await Opinion.getMessage(this.data.projectId)
    this.setData({
      formData: data
    })
  },
  // 切换tab
  handleTabChange: debounce( async function(e) {
    this.setData({
      tabIndex: e.detail.index
    })
    this.data.tabIndex = e.detail.index
    switch(this.data.tabIndex) {
      case 0: 
      console.log(this.data.tabIndex);
        // 基本信息
        break;
      case 1:
        console.log(this.data.tabIndex);
        // 进度信息
        break;
      case 2: 
        // 文件清单
        console.log(this.data.tabIndex);
        break;
      case 3:
        // 专家意见
        console.log(this.data.tabIndex);
        if(this.data.deptType == 1) {
          this._getMessageAPI()
        }
        break;
        case 4:
        // 讨论区
        console.log(this.data.tabIndex);
        break;
      default:
        break
    }
  }),
  handleScrollToTop: function () {
    wx.pageScrollTo({
        scrollTop: 0
    })
  },
  onUnload() {
    // 刷新页面数据
    const events =  this.getOpenerEventChannel()
    events.emit('onResetList')
  },
})